# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=subversion
pkgver=1.12.2
pkgrel=1
pkgdesc="A Modern Concurrent Version Control System"
arch=('i686' 'x86_64')
url="https://subversion.apache.org/"
license=('APACHE')
groups=('VCS')
depends=('libsqlite' 'file' 'liblz4' 'libserf' 'libsasl')
makedepends=('python2' 'perl' 'swig' 'ruby' 'liblz4-devel' 'libsqlite-devel' 'libserf-devel' 'libsasl-devel' 'gmp-devel')
optdepends=('bash-completion: for svn bash completion'
            'python2: for some hook scripts'
            'ruby: for some hook scripts')
provides=('svn')
options=('!makeflags' '!libtool' '!emptydirs')
noextract=("subversion-${pkgver}.tar.bz2")
source=(https://archive.apache.org/dist/subversion/subversion-${pkgver}.tar.bz2{,.asc}
        svnclean
        01-make-build-directories.patch
        02-fix-linking.patch
        03-fix-linking-perl.patch
        04-fix-switch-tests.patch
        05-retry-loop.patch
        06-svnadmin-binary-mode.patch
        07-fix-path-tests.patch
        08-ruby-test-fix.patch
        09-wincrypt.patch
        10-perl-vendors.patch
        11-XFAIL-case-sensitive-move-tests.patch
        12-fix-svnlook-test.patch
        13-dso_open.patch
        14-fix_svnauthz_tests.patch
        15-XFAIL_case_sensitive_tree_conflict.patch
        16-fix-swig3.patch
        17-fix-test-link.patch
        18-fix-serf-config.patch
        19-remove-contrib-from-configure.patch
        21-swig4-classic-deprecated.patch
        subversion-1.9.1-msys2.patch
        remove-checking-symlink.patch
        90-use-copy-instead-symlink.patch)
sha256sums=('3bd0b5c8e4c5175263dc9a92fd9aef94ce917e80af034f26fe5c45fde7e0f771'
            'SKIP'
            'b09dd041aba0078c8d50df130ef2f96c3ef8486279a620532fef4fe48ef9961e'
            'dfbd7b00ebdb3208614dc8622a99d3b12c0fd774f3207ac4d0e3b2d8a84b2a9e'
            '77fd514a60978fd9c5b2a5abfc051e22054685f4d25458f75f3c03aa5057e6aa'
            'f4da4254af50ea509d8007116c8e8aa21d83621acc8136d0f2df94be2cdad1cb'
            'c31590874160a3a434fedad2ce2eef8cd9580c661fe80c9f65566f0be7c900f9'
            '52d727ef76a4b1081dc6fa705becdee870ff63b06398ffa0986491fa6677658b'
            'c77f72a397e588d07390fe1d184cb2d54dc47d92d87019b8364a38512c2e80f3'
            '827309169ecd862df04b21754fd2155b4f35e15b726089834d753ae5edcce99d'
            '0da0af269761160244952d7f95ab17c5d4c35b0020044758d164df833b3e7ba9'
            'a40607183ab4ab1d54cb79493ac3f9801acfbbfe421d6dbb9915c2cdd1f64e86'
            '7efe22db531b5ba3fd4382378bde44416159b8d0960450d56a8d84361a17acc8'
            'e36cf59aaca71fb50b433e4ee89b356ea0684a0fb97d0056231e38cd5d3057a9'
            '4069a84189db7ed0d08c5fda927308d494ac9377e111d70debdbb6ad889af70b'
            '766143251f358791c7c1dd2a5339663ca1f41b13d2e8cae427ed0939acdb060a'
            '4d3c427c3bb423cd2a3ac244cd76694c9849035373c09018d0e0f2bffc595c54'
            'd480726924809f3586d6145433efc9e1633db751c28fc7be13abfd83328f1a20'
            '8682d9f7da2ba8607ed15daf3beec92e5dacda3a6e4c07baaef8991264eb44cd'
            '7152e0213f8e8e7a1b50349aaca57e7331b3eb239ada1afed617c22a7617578a'
            '484bf6d7f1440d7a8ee4a0bb00366937990694ed9a4a0f662225435df949aebc'
            'c7ebf4320b0147256a6b5ebab459603cbb7b8beb32da70b2ad85e6fb33609e30'
            '0e9a0f483bf53a105f3f53244ce3d03d6ed8c02f70cab1423e9e7c5ba44882e9'
            'f3d2e7e6ca97c8310fb59ce82430005fcd9e508b4a2a3f4ec7208011bf32a0a8'
            'd821304ee957ef4adbb0a8dec9cea3fe62ad7163aef132317d28f70ea25162ca'
            'fc545e375d8af4ef6d8afad453453480664b6a2ff74915f94b8a7da7de14155b')

PYTHON_SITELIB=$(/usr/bin/python2 -c 'from distutils.sysconfig import * ; print(get_python_lib(0,0));')

prepare() {
  cd ${startdir}/
  [[ -d ${srcdir}/${pkgname}-${pkgver} ]] && rm -rf -d ${srcdir}/${pkgname}-${pkgver}
  tar -xjvf ${SRCDEST}/${pkgname}-${pkgver}.tar.bz2 -C ${srcdir} || true

  cd ${srcdir}/${pkgname}-${pkgver}

  sed -i 's|/usr/bin/env python|/usr/bin/env python2|' \
    tools/hook-scripts/{,mailer/{,tests/}}*.py \
    tools/client-side/mergeinfo-sanitizer.py

  sed -i 's|/usr/bin/env python|/usr/bin/env python2|' gen-make.py build/getversion.py
  sed -i 's|except IOError,|except IOError as|' build/getversion.py

  patch -p1 -i ${srcdir}/01-make-build-directories.patch
  patch -p1 -i ${srcdir}/02-fix-linking.patch
  patch -p1 -i ${srcdir}/03-fix-linking-perl.patch
  patch -p1 -i ${srcdir}/04-fix-switch-tests.patch
  patch -p1 -i ${srcdir}/05-retry-loop.patch
  patch -p1 -i ${srcdir}/06-svnadmin-binary-mode.patch
  patch -p1 -i ${srcdir}/07-fix-path-tests.patch
  patch -p1 -i ${srcdir}/08-ruby-test-fix.patch
  patch -p1 -i ${srcdir}/09-wincrypt.patch
  patch -p1 -i ${srcdir}/10-perl-vendors.patch
  patch -p1 -i ${srcdir}/11-XFAIL-case-sensitive-move-tests.patch
  patch -p1 -i ${srcdir}/12-fix-svnlook-test.patch
  patch -p1 -i ${srcdir}/13-dso_open.patch
  patch -p1 -i ${srcdir}/14-fix_svnauthz_tests.patch
  patch -p1 -i ${srcdir}/15-XFAIL_case_sensitive_tree_conflict.patch
  patch -p1 -i ${srcdir}/16-fix-swig3.patch
  patch -p1 -i ${srcdir}/17-fix-test-link.patch
  patch -p1 -i ${srcdir}/18-fix-serf-config.patch
  patch -p1 -i ${srcdir}/19-remove-contrib-from-configure.patch
  patch -p1 -i ${srcdir}/21-swig4-classic-deprecated.patch
  patch -p1 -i ${srcdir}/subversion-1.9.1-msys2.patch
  patch -p1 -i ${srcdir}/remove-checking-symlink.patch
  patch -p1 -i ${srcdir}/90-use-copy-instead-symlink.patch

  PYTHON=/usr/bin/python2 ./autogen.sh
}

build() {
  cd ${pkgname}-${pkgver}
  export PYTHON=/usr/bin/python2
  ./configure --prefix=/usr \
      --build=${CHOST} \
      --with-apr=/usr \
      --with-apr-util=/usr \
      --with-zlib=/usr \
      --with-serf=/usr \
      --with-sqlite=/usr \
      --without-apxs \
      --without-gnome-keyring \
      --with-utf8proc=internal \
      --enable-shared --disable-static \
      --with-ruby-sitedir=/usr/lib/ruby/vendor_ruby \
      --disable-mod-activation \
      --enable-disallowing-of-undefined-references \
      --enable-local-library-preloading

  #make LT_LDFLAGS="-L$Fdestdir/usr/lib"
  # make swig_pydir=/usr/lib/python2.7/site-packages/libsvn \
  #  swig_pydir_extra=/usr/lib/python2.7/site-packages/svn swig-py swig-pl swig-rb

  make clean-swig
  make autogen-swig
  make all

  plain "Compiling swig bindings: ruby"
  make swig-rb

  plain "Compiling swig bindings: perl"
  make swig-pl

  plain "Compiling swig bindings: python"
  make swig-py swig_pydir=${PYTHON_SITELIB}/libsvn swig_pydir_extra=${PYTHON_SITELIB}/svn
}

#check() {
#   cd ${pkgname}-${pkgver}
#   export LANG=C LC_ALL=C
#   make check check-swig-pl check-swig-py check-swig-rb CLEANUP=yes # check-javahl
#}

package() {
  cd ${pkgname}-${pkgver}

  make -j1 DESTDIR="${pkgdir}" INSTALLDIRS=vendor \
    swig_pydir=${PYTHON_SITELIB}/libsvn \
    swig_pydir_extra=${PYTHON_SITELIB}/svn \
    install install-tools install-swig-py install-swig-pl install-swig-rb

  install -dm755 "${pkgdir}"/usr/share/subversion
  cp -a tools/hook-scripts "${pkgdir}"/usr/share/subversion/
  rm "${pkgdir}"/usr/share/subversion/hook-scripts/*.in

  ## svnclean
  install -Dm 755 ${srcdir}/svnclean "${pkgdir}"/usr/bin/svnclean

  install -Dm 644 tools/client-side/bash_completion \
     "${pkgdir}"/usr/share/bash-completion/completions/subversion
  for i in svn svnadmin svndumpfilter svnlook svnsync svnversion; do
      ln -sf subversion "${pkgdir}"/usr/share/bash-completion/completions/${i}
  done

  # Remove illegal files
  find ${pkgdir}/usr -type f -name "*::*" | xargs rm -f

  # fix permissons
  find ${pkgdir}/usr -type f \( -name *.dll -o -name *.exe \) | xargs chmod 0755
}
